package com.code.ContactAdvantage;

import java.util.ArrayList;
import java.util.Collections;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.code.ContactAdvantage.ContactAdvantage.Contact;
import com.code.ContactAdvantage.ContactAdvantage.StateInfo;
import com.code.SleepAlarm.R;


public class OurListActivity extends ListActivity implements OnItemClickListener, OnClickListener
{
	public static int index;
	public static StateInfo state;
	public static ArrayList<Contact> contacts;
	public static ArrayList<String> names;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		String targetState = extras.getString("state");
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        state = ContactAdvantage.stateToContact.get(targetState.toUpperCase());
        Log.e("State-out", targetState.toUpperCase());
        contacts = state.contacts;
        names = state.names;
        Collections.sort(contacts);
        Collections.sort(names);
        String []nameArray = new String[names.size()];
        names.toArray(nameArray);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, nameArray));
        
        lv.setOnItemClickListener(this);
        
	}

	public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	{	
		AlertDialog ad = new AlertDialog.Builder(this).setTitle(contacts.get(position).name).setMessage("What would you like to do?").create();
		index = position;
		ad.setButton("Call this dude", this);
		ad.setButton3("Cancel", this);
		ad.setButton2("Text", this);
		
		ad.show();
		
	}

	public void onClick(DialogInterface dialog, int which)
	{
		switch (which)
		{
			case -1:
				//call
				String phoneNumber = contacts.get(index).number;
				Uri uri = Uri.fromParts("tel", phoneNumber, null);
				Intent callIntent = new Intent(Intent.ACTION_CALL, uri);
				callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(callIntent);
				break;
				
			case -3:
				//cancel
				break;
				
			case -2:
				//text
				
				//Intent sendIntent = new Intent(Intent.ACTION_VIEW);
				String number = contacts.get(index).number;
				Uri smsUri = Uri.parse("tel:"+number);
				Log.e(number, number);
				Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
				//intent.putExtra("sms_body", "Hey are you in town?");
				intent.putExtra("compose_mode", true);
				intent.putExtra("address", number);
				intent.setType("vnd.android-dir/mms-sms");
				startActivity(intent);
				break;
		}
	}

}